import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:package_info_plus/package_info_plus.dart';

import '../../common/config.dart';
import '../../common/utils/app.dart';
import '../../common/utils/intent.dart';
import '../../generated/assets.dart';

/// The home screen
class HomeScreen extends StatefulWidget {
  /// Constructs a [HomeScreen]
  const HomeScreen({super.key});

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  final String title = Config.appName;

  final List<_Home> items = [
    _Home(
      cover: Assets.iconEarnProf,
      title: 'Professional ways to earn money',
    ),
    _Home(
      cover: Assets.iconBestWay,
      title: 'Best ways to make a passive income at home',
    ),
  ];

  final List<_Home> drawers = [
    _Home(cover: Assets.iconShare, title: 'Share With Friend'),
    // _Home(cover: Assets.iconRate, title: 'Rate this App'),
    // _Home(cover: Assets.iconPolicy, title: 'Privacy Policy'),
    _Home(cover: Assets.iconContact, title: 'Contact Us'),
    _Home(cover: Assets.iconFeedback, title: 'Feedback'),
    _Home(cover: Assets.iconAbout, title: 'About Us'),
  ];

  final scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: scaffoldKey,
      appBar: AppBar(
        backgroundColor: const Color(0xff4744d3),
        leading: GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: () {
            scaffoldKey.currentState?.openDrawer();
          },
          child: Padding(
            padding: const EdgeInsets.all(16),
            child: Image.asset(Assets.iconMenu),
          ),
        ),
        titleSpacing: 0,
        title: Text(title, style: const TextStyle(fontSize: 18)),
      ),
      body: SafeArea(
        child: Column(children: items.map(item).toList()),
      ),
      drawer: buildDrawer(),
    );
  }

  Widget item(_Home item) {
    var index = items.indexOf(item);
    return GestureDetector(
      onTap: () => onClickItem(index),
      behavior: HitTestBehavior.opaque,
      child: Container(
        height: 120,
        padding: const EdgeInsets.symmetric(horizontal: 8).copyWith(top: 8),
        child: Card(
          color: const Color(0xff232428),
          child: Row(
            children: [
              AspectRatio(
                aspectRatio: 1.335,
                child: Container(
                  decoration: BoxDecoration(
                    image: DecorationImage(
                      image: AssetImage(item.cover),
                      fit: BoxFit.fitHeight,
                    ),
                    borderRadius: const BorderRadius.only(
                      topLeft: Radius.circular(8),
                      bottomLeft: Radius.circular(8),
                    ),
                  ),
                ),
              ),
              const SizedBox(width: 4),
              Expanded(child: Text(item.title, textAlign: TextAlign.center)),
              Container(
                height: 50,
                margin: const EdgeInsets.symmetric(horizontal: 8),
                padding: const EdgeInsets.symmetric(horizontal: 10),
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  color: const Color(0xff4744d3),
                  borderRadius: BorderRadius.circular(8),
                ),
                child: const Text('Know More'),
              )
            ],
          ),
        ),
      ),
    );
  }

  Widget buildDrawer() {
    return SafeArea(
      child: Drawer(
        width: 280,
        shape: const RoundedRectangleBorder(),
        backgroundColor: const Color(0xff1d1d1d),
        child: Stack(
          alignment: Alignment.topCenter,
          children: [
            DrawerHeader(
              decoration: const BoxDecoration(color: Color(0xff4744d3)),
              child: Stack(
                alignment: Alignment.topCenter,
                children: [
                  Text(
                    title,
                    textAlign: TextAlign.center,
                    style: const TextStyle(color: Colors.white, fontSize: 18),
                  ),
                ],
              ),
            ),
            Positioned(
              top: 100,
              child: Image.asset(
                Assets.iconLogo,
                height: 120,
              ),
            ),
            Container(
              margin: const EdgeInsets.only(top: 240),
              child: ListView.builder(
                padding: EdgeInsets.zero,
                itemCount: drawers.length,
                itemBuilder: (context, index) {
                  var drawer = drawers[index];
                  return ListTile(
                    dense: true,
                    title: Text(
                      drawer.title,
                      style: const TextStyle(fontSize: 14),
                    ),
                    leading: Image.asset(
                      drawer.cover,
                      width: 20,
                      color: const Color(0xff4744d3),
                    ),
                    onTap: () => onClickDrawerItem(index),
                  );
                },
              ),
            ),
            // 添加其他菜单项
          ],
        ),
      ),
    );
  }

  void onClickItem(int index) {
    switch (index) {
      case 0:
        context.push('/professional_way');
      case 1:
        context.push('/best_way');
    }
  }

  void onClickDrawerItem(int index) async {
    final PackageInfo info = await appInfo();
    final appName = info.appName;
    final packageName = info.packageName;
    switch (index) {
      case 0:
        shareText(
          '''
Hey guys!, Explore this amazing $appName App 
Link is given below:
https://play.google.com/store/apps/details?id=$packageName
'''
              .trim(),
        );
        break;
      case 1:
        if (!context.mounted) return;
        context.push('/contact');
        break;
      case 2:
        if (!context.mounted) return;
        context.push('/feedback');
        break;
      case 3:
        if (!context.mounted) return;
        context.push('/about');
        break;
    }
    scaffoldKey.currentState?.closeDrawer();
  }
}

class _Home {
  final String cover;
  final String title;

  _Home({required this.cover, required this.title});
}
